import execjs
import requests
import base64
import datetime
import json
from Crypto.Cipher import AES
from Crypto.Util import Counter
from parsel.selector import Selector

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
}
cookies = {
}


def aes_decrypt(text, secretKey):
    key = secretKey.encode('utf-8')
    ciphertext = base64.b64decode(text)
    iv = ciphertext[:16]
    actual_ciphertext = ciphertext[16:]
    ctr = Counter.new(128, initial_value=int.from_bytes(iv, byteorder='big'))
    cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
    plaintext = cipher.decrypt(actual_ciphertext)
    return plaintext.decode('utf-8')


def get_articles(page, keyword):
    today_timestamp = int(datetime.datetime.today().replace(microsecond=0).timestamp())
    seven_days_ago = datetime.datetime.today() - datetime.timedelta(days=7)
    seven_days_ago_timestamp = int(seven_days_ago.replace(microsecond=0).timestamp())

    url = "https://www.jianyu360.cn/jyapi/jybx/core/fType/searchList"
    data = {
        "searchGroup": 1,
        "reqType": "lastNews",
        "pageNum": page,
        "pageSize": 50,
        "keyWords": f"{keyword}",
        "searchMode": 0,
        "bidField": "",
        "publishTime": f"{seven_days_ago_timestamp}-{today_timestamp}",
        "selectType": "title,content",
        "subtype": "",
        "exclusionWords": "",
        "buyer": "",
        "winner": "",
        "agency": "",
        "industry": "",
        "province": "",
        "city": "",
        "district": "",
        "buyerClass": "",
        "fileExists": "",
        "price": "",
        "buyerTel": "",
        "winnerTel": ""
    }
    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, cookies=cookies, data=data).json()
    secretKey = response['secretKey']
    ciphertext = response['data']
    with open('剑鱼标讯.js', 'r') as f:
        js_code = f.read()
    secret_key = execjs.compile(js_code).call('rsaDecrypt', secretKey)
    decrypt_text = aes_decrypt(ciphertext, secret_key)
    articles_list = json.loads(decrypt_text)['data']['list']
    for article in articles_list:
        article_title = article['title']
        article_id = article['id']
        article_url = f'https://www.jianyu360.cn/nologin/content/{article_id}.html'
        article_time = datetime.datetime.fromtimestamp(float(article['publishTime']))
        print(article_title, article_url, article_time)
        article_mes = {
            'article_title': article_title,
            'article_url': article_url,
            'article_id': article_id,
            'article_time': article_time

        }
        parse_article(article_mes)


def parse_article(article_mes):
    article_url = article_mes['article_url']
    article_title = article_mes['article_title']
    with open('剑鱼标讯.js', 'r') as f:
        js_code = f.read()
    token = execjs.compile(js_code).call('get_token')
    cookies['token'] = token
    response = requests.get(article_url, headers=headers, cookies=cookies).content.decode()
    response_html = Selector(response)
    article_content = response_html.xpath('//div[@class="acticle-content"]//text()').extract()
    print(article_title, article_content)


if __name__ == '__main__':
    page = 1
    keyword = '舆情'
    get_articles(page, keyword)
